package com.bnlr.lrparking.appui.publish;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.appui.chose.ChoseCityActivity;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.appui.publish.adapter.SearchPoiAdapter;
import com.bnlr.lrparking.view.ClearEditText;
import com.bnlr.lrparking.view.SampleEditTextWatcher;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class MapSearchActivity extends BaseActivity {
    private Unbinder unbinder;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.cancel)
    TextView tvCancel;
    @BindView(R.id.et_title_search)
    ClearEditText etSearch;
    @BindView(R.id.linerLayout)
    LinearLayout linearLayout;
    @BindView(R.id.suggest_rv)
    RecyclerView suggestRv;
    @BindView(R.id.ll_select_city)
    LinearLayout selectCity;
    private PoiSearch poiSearch;
    private List<PoiInfo> data = new ArrayList<>();
    private List<PoiInfo> suggestData = new ArrayList<>();
    private SearchPoiAdapter adapter;
    private SearchPoiAdapter suggestAdapter;
    SuggestionSearch suggestionSearch;
    private String currentCity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_search);
        unbinder = ButterKnife.bind(this);
        Intent intent = getIntent();
        PoiInfo info = intent.getParcelableExtra("current_info");
        if (info != null) {
            tvCity.setText(info.city);
            currentCity = info.city;
            data.add(0,info);
        }
        initRecyclerView();
        poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(poiListener);
        suggestionSearch = SuggestionSearch.newInstance();
        suggestionSearch.setOnGetSuggestionResultListener(listener);
        poiSearch.searchNearby(new PoiNearbySearchOption()
                .location(info.location)
                .pageCapacity(7)
                .radius(500)
                .keyword("停车"));
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        etSearch.addTextChangedListener(new SampleEditTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                if (s.length() != 0) {
                    linearLayout.setVisibility(View.VISIBLE);
                    suggestionSearch.requestSuggestion((new SuggestionSearchOption()
                            .citylimit(true)
                            .city(currentCity).keyword(s.toString())
                    ));
                } else {
                    linearLayout.setVisibility(View.GONE);
                }
            }
        });
        selectCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(MapSearchActivity.this, ChoseCityActivity.class)
                        .putExtra("from","map")
                        .putExtra("city_name", AppConfig.getInstance().getString("strCity","郑州市")),1500);
            }
        });
    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        suggestRv.setLayoutManager(new LinearLayoutManager(this));
        suggestRv.setItemAnimator(new DefaultItemAnimator());
        adapter = new SearchPoiAdapter(this,data);
        recyclerView.setAdapter(adapter);
        adapter.setOnPoiItemClickListener(new SearchPoiAdapter.OnPoiItemClickListener() {
            @Override
            public void onItemClick(View v, PoiInfo info) {
                Intent intent = new Intent();
                intent.putExtra("back_info",info);
                setResult(1005,intent);
                finish();
            }
        });
        suggestAdapter = new SearchPoiAdapter(this,suggestData);
        suggestRv.setAdapter(suggestAdapter);
        suggestAdapter.setOnPoiItemClickListener(new SearchPoiAdapter.OnPoiItemClickListener() {
            @Override
            public void onItemClick(View v, PoiInfo info) {
                Intent intent = new Intent();
                intent.putExtra("back_info",info);
                setResult(1005,intent);
                finish();
            }
        });
    }
    OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener(){

        @Override
        public void onGetPoiResult(PoiResult result){
            //获取POI检索结果
            if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
                return;
            }
            data.addAll(result.getAllPoi());
            adapter.setNewData(data);
        }
        @Override
        public void onGetPoiDetailResult(PoiDetailResult result){
            //获取Place详情页检索结果
        }
        @Override
        public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

        }
    };
    OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
        @Override
        public void onGetSuggestionResult(SuggestionResult res) {

            if (res == null || res.getAllSuggestions() == null) {
                return;
                //未找到相关结果
            }
            List<SuggestionResult.SuggestionInfo> list = res.getAllSuggestions();
            suggestData.clear();
            for (int i = 0; i < list.size(); i++) {
                PoiInfo poiInfo = new PoiInfo();
                if (!TextUtils.isEmpty(list.get(i).city) && !TextUtils.isEmpty(list.get(i).district)) {
                    poiInfo.location = list.get(i).pt;
                    poiInfo.name = list.get(i).key;
                    poiInfo.address = list.get(i).city + list.get(i).district;
                    poiInfo.city = list.get(i).city;
                    suggestData.add(poiInfo);
                }
            }
            suggestAdapter.setNewData(suggestData);
            //获取在线建议检索结果
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1500 && resultCode == 1600) {
            String city = data.getStringExtra("picked_city");
            if (city != null) {
                tvCity.setText(city);
                currentCity = city;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        poiSearch.destroy();
        suggestionSearch.destroy();
    }
}
